package main

import (
	"baidu_rpc/basic/globar"
	user "baidu_rpc/kitex_gen/baidu_rpc/user"
	"baidu_rpc/model"
	"baidu_rpc/util"
	"context"
	"errors"
	"fmt"
)

// UserServiceImpl implements the last service interface defined in the IDL.
type UserServiceImpl struct{}

// GetRegister implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetRegister(ctx context.Context, req *user.GetRegisterReq) (resp *user.GetRegisterResp, err error) {
	// TODO: Your code here...
	var u model.User
	u.Mobile = req.Mobile
	u.Password = util.Md5(req.Password)
	err = globar.DB.Where("mobile = ?", u.Mobile).Find(&u).Limit(1).Error
	if err != nil {
		return nil, errors.New("查询失败")
	}
	err = globar.DB.Create(&u).Error
	if err != nil {
		return nil, errors.New("添加失败")
	}
	resp = &user.GetRegisterResp{
		Id: int64(u.ID),
	}
	return
}

// GetLogin implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetLogin(ctx context.Context, req *user.GetLoginReq) (resp *user.GetLoginResp, err error) {
	// TODO: Your code here...
	var u model.User
	u.Mobile = req.Mobile

	err = globar.DB.Where("mobile = ?", u.Mobile).Find(&u).Limit(1).Error
	if err != nil {
		return nil, errors.New("查询失败")
	}
	fmt.Println("::::", u.Password)
	fmt.Println("1111", util.Md5(req.Password))
	if u.Mobile == util.Md5(req.Password) {
		return nil, errors.New("密码不一致")
	}

	resp = &user.GetLoginResp{
		Id: int64(u.ID),
	}
	return
}

// LocateByIP implements the UserServiceImpl interface.
func (s *UserServiceImpl) LocateByIP(ctx context.Context, req *user.LocateByIPReq) (resp *user.LocateByIPResp, err error) {
	// TODO: Your code here...
	var l model.LocateBy
	l = model.LocateBy{
		PassengerId: req.PassengerId,
		Ip:          req.Ip,
	}
	err = globar.DB.Create(&l).Error
	if err != nil {
		return nil, errors.New("添加失败")
	}
	resp = &user.LocateByIPResp{
		Address: l.Address,
		Lng:     l.Lng,
		Lat:     l.Lat,
		Status:  int32(l.Status),
	}
	return
}

// AddCommonLocation implements the UserServiceImpl interface.
func (s *UserServiceImpl) AddCommonLocation(ctx context.Context, req *user.AddCommonLocationReq) (resp *user.AddCommonLocationResp, err error) {
	// TODO: Your code here...
	var a model.LocateBy
	a = model.LocateBy{
		PassengerId: req.PassengerId,
		Address:     req.Address,
		Remark:      req.Remark,
		Lat:         req.Lat,
		Status:      int64(req.Status),
	}
	err = globar.DB.Create(&a).Error
	if err != nil {
		return nil, errors.New("添加失败")
	}
	resp = &user.AddCommonLocationResp{
		Msg: a.Remark,
	}
	return
}

// GetUserList implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetUserList(ctx context.Context, req *user.GetUserListReq) (resp *user.GetUserListResp, err error) {
	// TODO: Your code here...
	var u model.User
	cover, err := u.GetCover(globar.DB, req.Page, req.Size)
	if err != nil {
		return nil, err
	}
	resp = &user.GetUserListResp{
		Req: cover,
	}
	return
}
